package com.yunjian.admin.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;


/**
 * web ajax 通用返回结果定义
 * @author sonta
 *
 * @param <T> 返回的业务结果
 */
@ApiModel
public class WebResult<T> {
	
	// 返回结果代码
	@ApiModelProperty(value="返回结果代码")
	private String code;
	
	// 返回结果信息
	@ApiModelProperty(value="返回结果描述")
	private String msg;
	
	// 返回业务结果
	@ApiModelProperty(value="返回业务数据（一般只有code 为 0 时才有业务数据返回）")
	private T attach;
	
	/**
	 * 默认为非法操作
	 */
	public WebResult() {
		this.code = Coder.ILLEGAL.getCode();
		this.msg = Coder.ILLEGAL.getMsg();
	}

	public T getAttach() {
		return attach;
	}

	public WebResult<T> setAttach(T attach) {
		this.attach = attach;
		return this;
	}
	
	public String getCode() {
		return code;
	}

	public WebResult<T> setCode(String code) {
		this.code = code;
		return this;
	}

	public String getMsg() {
		return msg;
	}

	public WebResult<T> setMsg(String msg) {
		this.msg = msg;
		return this;
	}
	
	public WebResult<T> setCoder(Coder coder) {
		this.code = coder.getCode();
		this.msg = coder.getMsg();
		return this;
	}


	public static  enum Coder {
		SUCCESS("0", "成功"),
		C403("403", "无此权限"),
		C404("404", "服务不存在"),
		C500("500", "服务端错误"),
		C1001("1001", "未知账户"),
		C1002("1002", "用户名或密码错误"),
		C1003("1003", "未获取验证码"),
		C1004("1004", "验证码不匹配"),
		C1005("1005", "用户已锁定"),
		C1006("1006", "用户已停用"),
		C1007("1007", "获取验证码过于频繁"),
                C1211("1211", "登录名已被占用, 请更换"),
                C1212("1212", "邮箱已被占用, 请更换"),
                C1213("1213", "该手机号已注册, 请更换"),
		C2001("2001", "写入数据失败"),
		C2002("2002", "更新数据失败"),
		C2003("2003", "删除数据失败"),
		C2004("2004", "未查询到相关数据"),
		C9000("9000", "未登录或登录超时"),
		C9001("9001", "无此权限"),
		ILLEGAL("9990", "非法操作"),
		ERROR("9999", "系统错误");
		private String code;
		private String msg;
		private Coder (String code, String msg) {
			this.code = code;
			this.msg = msg;
		}
		public String getCode() {
			return code;
		}
		public void setCode(String code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
		
		public static  String getCoderStr() {
			StringBuffer sb = new StringBuffer();
			sb.append("[").append("\n");
			int i = 0;
			for (Coder coder : Coder.values()) {
				if (i != 0) {
					sb.append(",\n");
				}
				sb.append("{code : ").append(coder.getCode()).append(", msg : ").append(coder.getMsg()).append("}");
				i++;
			}
			sb.append("\n").append("]");
			return sb.toString();
		}
	}
	
}
